<?php

namespace app\api\controller;

use app\common\controller\Api;

use think\Db;
class Timetask extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    // 每日00:05执行
    public function initDay() {
        $this->sendMsg();
    }

    protected function sendMsg()
    {
        $today = date('Y-m-d',time());
        $userList = Db::name('user_course')->where('term_id',1)->column('user_id');
       // dump($userList);die;
        $termList = Db::name('term')
            //->whereTime('stime', '<=', $today)
            //->whereTime('etime', '>=', $today)
            ->where('type', '<>', 2)
            ->where('deletetime',null)
            ->where('status','0')
            ->field('id,user_id')
            ->select()
            ->toArray();
        $openidList = [];
        foreach ($termList as $v) {
            $openidList[] .=  Db::name('user')->where('id',$v['user_id'])->value('openid');
            $userList = Db::name('user_course')->where('term_id',$v['id'])->value('user_id');
           // $userList = Db::name('user_course')->where('term_id',$v['id'])->where('status', '0')->value('user_id');
            foreach ($userList as $uid) {
                $openid = Db::name('user')->where('id',$uid)->value('openid');
                if (!$openid) {
                    $pid = Db::name('user')->where('id',$uid)->value('pid');
                    $openid = Db::name('user')->where('id',$pid)->value('openid');
                }
                $openidList[] .= $openid;
            }
        }

        $appid = Config('wx.mini_appid');
        $appsecret = Config('wx.mini_secret');
        $touser = $openid;
        $template_id = Config('wx.course_template_id');
        $page = $request->page;
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;

        $res = json_decode(file_get_contents($url),true);

        $access_token = $res['access_token'] ;

        //请求url
        $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token ;

        //发送内容
        $data = [] ;

        //接收者（用户）的 openid
        $data['touser'] = $touser;

        //所需下发的订阅模板id
        $data['template_id'] = $template_id ;

        //点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。
        $data['page'] = $page ;

        //模板内容，格式形如 { "key1": { "value": any }, "key2": { "value": any } }
        $data['data'] = [
            "character_string1"=>[
                'value' => '-----'
            ],
            "thing2"=>[
                'value' => '公证处摇号'
            ],
            "time3"=>[
                'value' => date("Y-m-d H:i:s")
            ],
            'phrase4'=>[
                'value'=>'摇号结束'
            ],
            'thing5'=>[
                'value'=>'摇号结束,进小程序查看'
            ]
        ];

        //跳转小程序类型：developer为开发版；trial为体验版；formal为正式版；默认为正式版
        $data['miniprogram_state'] = 'formal' ;

        return curl_post($url,json_encode($data), array("Content-Type:application/json")) ;
    }
}
?>